package com.example.lynandroid.activity

import android.os.Bundle
import androidx.databinding.DataBindingComponent
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.FragmentActivity
import com.example.lynandroid.enum.LifeType
import com.example.lynandroid.lifeLog

open class BaseActivity : FragmentActivity() {

    open fun getActivityName(): String? {
        return null
    }

    open fun getTheBinding(): ViewDataBinding? {
        return null
    }

    open lateinit var binding: ViewDataBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (getTheBinding() != null) {
            setContentView(getTheBinding()?.root)
        }

        log(LifeType.Create)
    }

    override fun onRestart() {
        super.onRestart()
        log(LifeType.Restart)
    }

    override fun onStart() {
        super.onStart()
        log(LifeType.Start)
    }

    override fun onResume() {
        super.onResume()
        log(LifeType.Resume)
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        log(LifeType.AttachToWindow)
    }

    override fun onWindowFocusChanged(hasFocus: Boolean) {
        super.onWindowFocusChanged(hasFocus)
        log(LifeType.WindowFocusChange)
        hasWindowFocus()
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        log(LifeType.OnSaveInstanceState)
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        log(LifeType.DetachFromWindow)
    }

    override fun onPause() {
        super.onPause()
        log(LifeType.Pause)
    }

    override fun onStop() {
        super.onStop()
        log(LifeType.Stop)
    }

    override fun onDestroy() {
        super.onDestroy()
        log(LifeType.Destroy)
    }

    private fun log(lifeType: LifeType) {
        lifeLog(getActivityName(), lifeType)
    }
}